import java.util.List;

import javax.persistence.*;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.HbmBinder;
import domain.*;
import domain.Class;
import domain.nativesql.Order;
import domain.uuid.Animal;


public class HibernateUtil {
	@PersistenceContext
	@Transient
	EntityManager entityManager;
	
	private static final SessionFactory sessionFactory = buildSessionFactory();
	//private EntityManagerFactory 
	@SuppressWarnings("deprecation")
	private static EntityManagerFactory buildEnFactory() {
		try {
			return new org.hibernate.ejb.Ejb3Configuration().configure("javadb.cfg.xml").createEntityManagerFactory();
		} catch (Throwable ex) {
			// Make sure you log the exception, as it might be swallowed
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}
	private static SessionFactory buildSessionFactory() {
		try {
			// Create the SessionFactory from hibernate.cfg.xml
			return new org.hibernate.cfg.AnnotationConfiguration().configure("javadb.cfg.xml").buildSessionFactory();
		} catch (Throwable ex) {
			// Make sure you log the exception, as it might be swallowed
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	// EntityManagerFactory emf =
	// Persistence.createEntityManagerFactory("OrderEM");
	public static void main(String args[]) {
	
		new HibernateUtil().testUUID();
	}
	
	public  void testNativeSQL()
	{
		
		EntityManagerFactory emf = buildEnFactory(); 
		EntityManager entityManager = emf.createEntityManager();
		 
		Query q = entityManager.createNativeQuery(" select oid as order_id,quantity as order_quantity,iid as item_id,name as item_name,itemid as order_itemid" +
				" from aorder,item where aorder.itemid = item.iid"
		,"order_item");
		
		q.setMaxResults(2);
		List resultList = q.getResultList();
		
		/*Order o = entityManager.find(Order.class, 1);
		System.out.println(o.getItem().getName());*/
		
		//javax.persistence.spi.
	}
	
	public  void testHib()
	{
		// System.out.println(stu.getName());
		SessionFactory sf = buildSessionFactory();
		Session session = sf.openSession();

		// Student zs = new Student();
		Student testLoadStudent = (Student) session.load(Student.class, 1);
		Class testLoadClass = (Class) session.load(Class.class, 2);
		Student bs = new BoyStudent();
		Address add = new Address();
		//TheClass gcTest = new GoodClass();
		GoodClass gcTest = (GoodClass) session.load(GoodClass.class, 3);
		System.out.println(gcTest.getClassName());
		for (Student stu : testLoadClass.getStudents()) {
			System.out.println(testLoadClass.getClassName() + " has student named  "
					+ stu.getName());
		}
		
		System.out.println(testLoadStudent.getName());
		Transaction tr = session.getTransaction();
		tr.begin();
		
		// zs.setHeight(30);
		// zs.setWidth(35);
		//
		//gcTest.setClassName("BestClass");
		//session.save(gcTest);
		//session.save(zs);
		//session.save(bs);
		
		tr.commit();
		
		session.close();
		
		System.out.println(testLoadStudent.getTheClass().getClassName());
		System.out.println("The height is: " + testLoadStudent.getHeight());
		// session.createQuery(arg0)
	}
	
	public static void testUUID()
	{
		SessionFactory sf = buildSessionFactory();
		Session session = sf.openSession();
		Transaction tr = session.getTransaction();
		
		tr.begin();
	    
		session.save(new Animal());
		
		tr.commit();
		
		session.close();
	}
}